package com.magic.web.common.controller;


import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import sun.net.www.protocol.http.HttpURLConnection;

import java.io.InputStream;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description
 * @author wm
 * @date 2021/3/25
 */

public class testUtil {

    private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    public static Set<String> getHolidays(int year, int month) throws Exception {
        Set<String> monthWeekOfDay = getMonthWeekOfDay(year, month);
        Map jjr = getJJR(year, month);
        Integer code = (Integer) jjr.get("code");
        if (code != 0) {
            return monthWeekOfDay;
        }
        Map<String, Map<String, Object>> holiday = (Map<String, Map<String, Object>>) jjr.get("holiday");
        Set<String> keys = holiday.keySet();
        for (String key : keys) {
            Map<String, Object> vlaue = holiday.get(key);
            String data = (String) vlaue.get("date");
            Integer wage = (Integer) vlaue.get("wage");//1代表1倍工资,2代表双倍工资,3代表3倍工资
            if (1 == wage) {
                monthWeekOfDay.remove(data);
            } else {
                monthWeekOfDay.add(data);
            }
        }
        return monthWeekOfDay;
    }

    //获取节假日
    public static Map getJJR(int year, int month) throws Exception {
        String Url = "http://timor.tech/api/holiday/year/" + year + "-" + month;
        //String Url = "http://opendata.baidu.com/api.php?query="+year + "年" + month+"月&resource_id=6018&format=json";
        //String Url = "http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query="+year + "-" + month+"&resource_id=6018&format=json";
        StringBuilder responseStr = new StringBuilder();
        URL url = new URL(Url);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //设置请求方式
        connection.setRequestMethod("GET");
        //连接
        System.setProperty("http.proxyHost", "42.1.39.160");
        System.setProperty("http.proxyPort", "808");
        //System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "false");
        //System.setProperty("jdk.http.auth.proxying.disabledSchemes", "false");
        Authenticator.setDefault(new BasicAuthenticator("shihaibin", "110450"));
        /*Authenticator.setDefault(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("shihaibin", "110450".toCharArray());
            }
        });*/
        connection.setRequestProperty("accept", "*/*");
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)");
        connection.connect();
        //得到响应码
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            //得到响应流
            InputStream inputStream = connection.getInputStream();
            //将响应流转换成字符串
            List<String> responseList = IOUtils.readLines(inputStream, "UTF-8");
            for (String response : responseList) {
                responseStr.append(response);
            }
        }
        JSONObject obj = JSONObject.parseObject(responseStr + "");
        connection.disconnect();
        return obj;
    }

    //获取所有周末的接口
    public static Set<String> getMonthWeekOfDay(int year, int month) {
        Set<String> list = new HashSet<>();
        //获取当前月的1号
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        //获取下个月1号
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.set(year, month, 1);

        while (true) {
            int weekday = calendar.get(Calendar.DAY_OF_WEEK);
            //只取周六和周日
            if (weekday == 1 || weekday == 7) {
                list.add(format.format(calendar.getTime()));
            }
            calendar.add(Calendar.DATE, 1);
            if (calendar.getTimeInMillis() >= endCalendar.getTimeInMillis()) {
                break;
            }
        }
        return list;
    }


    //获取节假日
    public static Map getJJR1(int year, int month) throws Exception {
        String url = "http://timor.tech/api/holiday/year/" + year + "-" + month;
        String requeststr = HttpUtils.sendMessageByGetWithUTF8(url);
        JSONObject jsonObject = JSONObject.parseObject(requeststr);
        return jsonObject;
    }

    public static void main(String[] args) throws Exception {
        Map jjr = getJJR(2021, 4);
        System.out.println(jjr);
        //Set<String> holidays = getHolidays(2021, 3);
        //System.out.println(holidays);
    }
}
